package com.generator.marketManage.customerManage.service.impl;

import com.generator.marketManage.customerManage.dao.MarketCustomManageMapper;
import com.generator.marketManage.customerManage.pojo.City;
import com.generator.marketManage.customerManage.pojo.MarketCustomManage;
import com.generator.marketManage.customerManage.pojo.Users;
import com.generator.marketManage.customerManage.service.MarketCustomManageService;

import com.generator.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 88168
 * @since 2022-10-04
 */
@Service
public class MarketCustomManageMapperImpl implements MarketCustomManageService {

    @Autowired
    private MarketCustomManageMapper mapper;

    /**
     * @param marketCustomManage
     * @return 查询所有用户信息
     */
    @Override
    public PageModel<MarketCustomManage> getMarketCustom(MarketCustomManage marketCustomManage, PageModel<MarketCustomManage> pageModel) {
        pageModel.setPageNum(pageModel.getPageNum() <= 0 ? 1 : pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize() <= 0 ? 3 : pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());
        List<MarketCustomManage> list = mapper.getMarketCustom(marketCustomManage);
        PageInfo<MarketCustomManage> pageInfo = new PageInfo<MarketCustomManage>(list);
        pageModel.setData(new ArrayList<MarketCustomManage>(list));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());
        return pageModel;
    }
    /**
     * @param customName
     * @return 查询指定用户信息
     */
    @Override
    public MarketCustomManage getcustomOne(String customName) {
        System.out.println("*******************************************");
        return mapper.getcustomOne(customName);
    }

    @Override
    /*
     * 表明该类（class）或方法（method）受事务控制
     * @param propagation  设置隔离级别
     * @param isolation 设置传播行为
     * @param rollbackFor 设置需要回滚的异常类，默认为RuntimeException
     * 注销
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public String logout(String customName) {
        mapper.logout(customName);
        return "注销成功";
    }

    @Override
    /*
     ** 恢复
     * 表明该类（class）或方法（method）受事务控制
     * @param propagation  设置隔离级别
     * @param isolation 设置传播行为
     * @param rollbackFor 设置需要回滚的异常类，默认为RuntimeException
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public String refresh(String custName) {
        mapper.refresh(custName);
        return "恢复成功";
    }

    /**
     * 更新客户表
     * @param marketCustomManage
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public String updateCustom(MarketCustomManage marketCustomManage) {
        City city = mapper.selectAddress(marketCustomManage.getCname());
        marketCustomManage.setCID(city.getId());
        marketCustomManage.setCustomid(Integer.parseInt(marketCustomManage.getCustomidString()));
        mapper.updateCustom(marketCustomManage);
        return "修改成功";
    }

    /**
     * @param marketCustomManage
     * @return 添加custom
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public String addCustom(MarketCustomManage marketCustomManage)  {
        Date date = new Date();
        marketCustomManage.setCreatetime1(date);
        Users users = mapper.selectCreateId(marketCustomManage.getCreatedPerson());
        marketCustomManage.setCreatedPersonId(users.getUId());
        City city = mapper.selectAddress(marketCustomManage.getCname());
        marketCustomManage.setCID(city.getId());
        mapper.addCustom(marketCustomManage);
        return "添加成功";
    }

    /**
     *查询市场组员
     * @param userName
     * @return
     */

    @Override
    public List<Users> selectMarketMembers(String userName) {
        return mapper.selectMarketMembers(userName);
    }
    /**
     * 分配给-市场组员
     *
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public String updateDistribute(MarketCustomManage marketCustomManage) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        System.out.println("**********************************");
        System.out.println("_________:"+format);
        marketCustomManage.setCreatetime1String(format);
        List<Users> users = mapper.selectMarketMembers(marketCustomManage.getServicePerson());
        marketCustomManage.setServicePersonId( users.get(0).getUId());
        mapper.updateDistribute(marketCustomManage);
        return "分配成功";
    }

    /**
     * 多条件查询
     * @param customManage
     * @param pageModel
     * @return
     */
    @Override
    public PageModel<MarketCustomManage> getMoreCondition(MarketCustomManage customManage, PageModel<MarketCustomManage> pageModel) {
        pageModel.setPageNum(pageModel.getPageNum() <= 0 ? 1 : pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize() <= 0 ? 3 : pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());
        List<MarketCustomManage> list = mapper.getMoreCondition(customManage);
        PageInfo<MarketCustomManage> pageInfo = new PageInfo<MarketCustomManage>(list);
        pageModel.setData(new ArrayList<MarketCustomManage>(list));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());
        return pageModel;
    }


}
